import { 
  getMessages, 
  getUnreadCount, 
  getMessage, 
  markAsRead, 
  batchMarkAsRead, 
  markAllAsRead, 
  deleteMessage, 
  batchDeleteMessages 
} from '@/api/message'

const state = {
  messages: [],
  unreadCount: 0,
  currentMessage: null,
  loading: false,
  totalCount: 0
}

const mutations = {
  SET_MESSAGES(state, messages) {
    state.messages = messages
  },
  SET_UNREAD_COUNT(state, count) {
    state.unreadCount = count
  },
  SET_CURRENT_MESSAGE(state, message) {
    state.currentMessage = message
  },
  SET_LOADING(state, status) {
    state.loading = status
  },
  SET_TOTAL_COUNT(state, count) {
    state.totalCount = count
  }
}

const actions = {
  // 获取消息列表
  async fetchMessages({ commit }, params = { page: 1, limit: 10, type: 'all' }) {
    commit('SET_LOADING', true)
    try {
      const response = await getMessages(params)
      commit('SET_MESSAGES', response.list)
      commit('SET_TOTAL_COUNT', response.total)
      return response
    } catch (error) {
      console.error('获取消息列表失败', error)
      throw error
    } finally {
      commit('SET_LOADING', false)
    }
  },

  // 获取未读消息数量
  async fetchUnreadCount({ commit }) {
    try {
      const count = await getUnreadCount()
      commit('SET_UNREAD_COUNT', count)
      return count
    } catch (error) {
      console.error('获取未读消息数量失败', error)
      throw error
    }
  },

  // 获取单条消息详情
  async fetchMessage({ commit }, id) {
    commit('SET_LOADING', true)
    try {
      const message = await getMessage(id)
      commit('SET_CURRENT_MESSAGE', message)
      return message
    } catch (error) {
      console.error('获取消息详情失败', error)
      throw error
    } finally {
      commit('SET_LOADING', false)
    }
  },

  // 标记单条消息为已读
  async readMessage({ commit, dispatch }, id) {
    try {
      await markAsRead(id)
      // 重新获取未读消息数量
      dispatch('fetchUnreadCount')
      return true
    } catch (error) {
      console.error('标记消息已读失败', error)
      throw error
    }
  },

  // 批量标记消息为已读
  async readMessages({ commit, dispatch }, ids) {
    try {
      await batchMarkAsRead(ids)
      // 重新获取未读消息数量
      dispatch('fetchUnreadCount')
      return true
    } catch (error) {
      console.error('批量标记消息已读失败', error)
      throw error
    }
  },

  // 标记所有消息为已读
  async readAllMessages({ commit, dispatch }, type) {
    try {
      await markAllAsRead(type)
      commit('SET_UNREAD_COUNT', 0)
      return true
    } catch (error) {
      console.error('标记所有消息已读失败', error)
      throw error
    }
  },

  // 删除单条消息
  async removeMessage({ commit, dispatch, state }, id) {
    try {
      await deleteMessage(id)
      // 更新消息列表，简单处理，直接重新获取
      const params = { page: 1, limit: state.messages.length, type: 'all' }
      dispatch('fetchMessages', params)
      return true
    } catch (error) {
      console.error('删除消息失败', error)
      throw error
    }
  },

  // 批量删除消息
  async removeMessages({ commit, dispatch, state }, ids) {
    try {
      await batchDeleteMessages(ids)
      // 更新消息列表，简单处理，直接重新获取
      const params = { page: 1, limit: state.messages.length, type: 'all' }
      dispatch('fetchMessages', params)
      return true
    } catch (error) {
      console.error('批量删除消息失败', error)
      throw error
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
} 